# Copied from https://skia.googlesource.com/skia/+/refs/heads/main/bazel/external/libwebp/BUILD.bazel
# The copied code is available under the following BSD-style license (see
# https://skia.googlesource.com/skia/+/refs/heads/main/LICENSE):
#
# Copyright (c) 2011 Google Inc. All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#   * Neither the name of the copyright holder nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# This file will be copied into //third_party/externals/libwebp via the new_local_repository
# rule in WORKSPACE.bazel, so all files should be relative to that path.
# We define this here because the emscripten toolchain calls the cpu wasm, whereas the
# bazelbuild/platforms call it wasm32. https://github.com/emscripten-core/emsdk/issues/919

load("@rules_cc//cc:cc_library.bzl", "cc_library")

config_setting(
    name = "cpu_wasm",
    values = {
        "cpu": "wasm",
    },
)

WEBP_SRCS = [
    "sharpyuv/sharpyuv.c",
    "sharpyuv/sharpyuv.h",
    "sharpyuv/sharpyuv_cpu.c",
    "sharpyuv/sharpyuv_cpu.h",
    "sharpyuv/sharpyuv_csp.c",
    "sharpyuv/sharpyuv_csp.h",
    "sharpyuv/sharpyuv_dsp.c",
    "sharpyuv/sharpyuv_dsp.h",
    "sharpyuv/sharpyuv_gamma.c",
    "sharpyuv/sharpyuv_gamma.h",
    "sharpyuv/sharpyuv_neon.c",
    "sharpyuv/sharpyuv_sse2.c",
    "src/dec/alpha_dec.c",
    "src/dec/alphai_dec.h",
    "src/dec/buffer_dec.c",
    "src/dec/common_dec.h",
    "src/dec/frame_dec.c",
    "src/dec/idec_dec.c",
    "src/dec/io_dec.c",
    "src/dec/quant_dec.c",
    "src/dec/tree_dec.c",
    "src/dec/vp8_dec.c",
    "src/dec/vp8_dec.h",
    "src/dec/vp8i_dec.h",
    "src/dec/vp8l_dec.c",
    "src/dec/vp8li_dec.h",
    "src/dec/webp_dec.c",
    "src/dec/webpi_dec.h",
    "src/demux/anim_decode.c",
    "src/demux/demux.c",
    "src/dsp/alpha_processing.c",
    "src/dsp/alpha_processing_mips_dsp_r2.c",
    "src/dsp/alpha_processing_neon.c",
    "src/dsp/alpha_processing_sse2.c",
    "src/dsp/common_sse2.h",
    "src/dsp/common_sse41.h",
    "src/dsp/cost.c",
    "src/dsp/cost_mips32.c",
    "src/dsp/cost_mips_dsp_r2.c",
    "src/dsp/cost_neon.c",
    "src/dsp/cost_sse2.c",
    "src/dsp/cpu.c",
    "src/dsp/cpu.h",
    "src/dsp/dec.c",
    "src/dsp/dec_clip_tables.c",
    "src/dsp/dec_mips32.c",
    "src/dsp/dec_mips_dsp_r2.c",
    "src/dsp/dec_msa.c",
    "src/dsp/dec_neon.c",
    "src/dsp/dec_sse2.c",
    "src/dsp/dsp.h",
    "src/dsp/enc.c",
    "src/dsp/enc_mips32.c",
    "src/dsp/enc_mips_dsp_r2.c",
    "src/dsp/enc_msa.c",
    "src/dsp/enc_neon.c",
    "src/dsp/enc_sse2.c",
    "src/dsp/filters.c",
    "src/dsp/filters_mips_dsp_r2.c",
    "src/dsp/filters_msa.c",
    "src/dsp/filters_neon.c",
    "src/dsp/filters_sse2.c",
    "src/dsp/lossless.c",
    "src/dsp/lossless.h",
    "src/dsp/lossless_common.h",
    "src/dsp/lossless_enc.c",
    "src/dsp/lossless_enc_mips32.c",
    "src/dsp/lossless_enc_mips_dsp_r2.c",
    "src/dsp/lossless_enc_msa.c",
    "src/dsp/lossless_enc_neon.c",
    "src/dsp/lossless_enc_sse2.c",
    "src/dsp/lossless_mips_dsp_r2.c",
    "src/dsp/lossless_msa.c",
    "src/dsp/lossless_neon.c",
    "src/dsp/lossless_sse2.c",
    "src/dsp/mips_macro.h",
    "src/dsp/msa_macro.h",
    "src/dsp/neon.h",
    "src/dsp/quant.h",
    "src/dsp/rescaler.c",
    "src/dsp/rescaler_mips32.c",
    "src/dsp/rescaler_mips_dsp_r2.c",
    "src/dsp/rescaler_msa.c",
    "src/dsp/rescaler_neon.c",
    "src/dsp/rescaler_sse2.c",
    "src/dsp/ssim.c",
    "src/dsp/ssim_sse2.c",
    "src/dsp/upsampling.c",
    "src/dsp/upsampling_mips_dsp_r2.c",
    "src/dsp/upsampling_msa.c",
    "src/dsp/upsampling_neon.c",
    "src/dsp/upsampling_sse2.c",
    "src/dsp/yuv.c",
    "src/dsp/yuv.h",
    "src/dsp/yuv_mips32.c",
    "src/dsp/yuv_mips_dsp_r2.c",
    "src/dsp/yuv_neon.c",
    "src/dsp/yuv_sse2.c",
    "src/enc/alpha_enc.c",
    "src/enc/analysis_enc.c",
    "src/enc/backward_references_cost_enc.c",
    "src/enc/backward_references_enc.c",
    "src/enc/backward_references_enc.h",
    "src/enc/config_enc.c",
    "src/enc/cost_enc.c",
    "src/enc/cost_enc.h",
    "src/enc/filter_enc.c",
    "src/enc/frame_enc.c",
    "src/enc/histogram_enc.c",
    "src/enc/histogram_enc.h",
    "src/enc/iterator_enc.c",
    "src/enc/near_lossless_enc.c",
    "src/enc/picture_csp_enc.c",
    "src/enc/picture_enc.c",
    "src/enc/picture_psnr_enc.c",
    "src/enc/picture_rescale_enc.c",
    "src/enc/picture_tools_enc.c",
    "src/enc/predictor_enc.c",
    "src/enc/quant_enc.c",
    "src/enc/syntax_enc.c",
    "src/enc/token_enc.c",
    "src/enc/tree_enc.c",
    "src/enc/vp8i_enc.h",
    "src/enc/vp8l_enc.c",
    "src/enc/vp8li_enc.h",
    "src/enc/webp_enc.c",
    "src/mux/anim_encode.c",
    "src/mux/animi.h",
    "src/mux/muxedit.c",
    "src/mux/muxi.h",
    "src/mux/muxinternal.c",
    "src/mux/muxread.c",
    "src/utils/bit_reader_inl_utils.h",
    "src/utils/bit_reader_utils.c",
    "src/utils/bit_reader_utils.h",
    "src/utils/bit_writer_utils.c",
    "src/utils/bit_writer_utils.h",
    "src/utils/color_cache_utils.c",
    "src/utils/color_cache_utils.h",
    "src/utils/endian_inl_utils.h",
    "src/utils/filters_utils.c",
    "src/utils/filters_utils.h",
    "src/utils/huffman_encode_utils.c",
    "src/utils/huffman_encode_utils.h",
    "src/utils/huffman_utils.c",
    "src/utils/huffman_utils.h",
    "src/utils/palette.c",
    "src/utils/palette.h",
    "src/utils/quant_levels_dec_utils.c",
    "src/utils/quant_levels_dec_utils.h",
    "src/utils/quant_levels_utils.c",
    "src/utils/quant_levels_utils.h",
    "src/utils/random_utils.c",
    "src/utils/random_utils.h",
    "src/utils/rescaler_utils.c",
    "src/utils/rescaler_utils.h",
    "src/utils/thread_utils.c",
    "src/utils/thread_utils.h",
    "src/utils/utils.c",
    "src/utils/utils.h",
] + select({
    "@platforms//cpu:x86_64": [
        "src/dsp/alpha_processing_sse41.c",
        "src/dsp/dec_sse41.c",
        "src/dsp/enc_sse41.c",
        "src/dsp/lossless_enc_sse41.c",
        "src/dsp/lossless_sse41.c",
        "src/dsp/upsampling_sse41.c",
        "src/dsp/yuv_sse41.c",
    ],
    ":cpu_wasm": [],  # not sure why wasm doesn't use default
    "//conditions:default": [],
})

WEBP_COPTS = select({
    "@platforms//os:windows": [],
    "//conditions:default": ["-Wno-unused-but-set-variable"],
}) + select({
    "@platforms//cpu:x86_64": ["-msse4.1"],
    ":cpu_wasm": [],  # not sure why wasm doesn't use default
    "//conditions:default": [],
})

WEBP_DEFINES = [
    # TODO(scroggo): swizzle ourself in SkWebpCodec instead of requiring this non-standard libwebp.
    "WEBP_SWAP_16BIT_CSP",
]

cc_library(
    name = "libwebp",
    srcs = WEBP_SRCS,
    hdrs = [
        "src/webp/decode.h",
        "src/webp/demux.h",
        "src/webp/encode.h",
        "src/webp/format_constants.h",
        "src/webp/mux.h",
        "src/webp/mux_types.h",
        "src/webp/types.h",
    ],
    copts = WEBP_COPTS,
    includes = ["src"],
    local_defines = WEBP_DEFINES,
    textual_hdrs = ["src/dsp/cpu.c"],
    visibility = ["//visibility:public"],
)
